//Ajax cache array
var ajax_cache = new Array();

function loadPage(page, action, div) {
    //Check if file has been called before
    if(page + '_' + action in ajax_cache) {
        //load file content from the Ajax cache
        document.getElementById(div).innerHTML = ajax_cache[page + "_" + action];
    }
    else {
        //AJAX Call code
        var xmlhttp;
        if(window.XMLHttpRequest)
        {
                // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
        }
        else
        {
                // code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                        document.getElementById(div).innerHTML=xmlhttp.responseText;
                        //Cache AJAX call
                        ajax_cache[page + "_" + action] = xmlhttp.responseText;
                }
        }
        var url = "controller/controller.php?page=" + page;
        if(action != "") {
            url = url + "&action=" + action;
        }
        xmlhttp.open("POST",url,true);
        xmlhttp.send();
    }
}

//Check if browser supports webworkers
if(typeof(Worker)!=="undefined")
{
    //Start a webworker which loads webpages in the ajax_cache
    var pageloader = new Worker('scripts/ww_pageloader.js');
    pageloader.onmessage = function (e) {            
        ajax_cache[e.data.page_action] = e.data.content;

    };
    pageloader.postMessage(ajax_cache);
}
